package cn.wolfcode.crud.service.impl;

import cn.wolfcode.crud.domain.CourseOrder;
import cn.wolfcode.crud.domain.Customer;
import cn.wolfcode.crud.domain.Employee;
import cn.wolfcode.crud.mapper.CourseOrderMapper;
import cn.wolfcode.crud.qo.QueryObject;
import cn.wolfcode.crud.service.ICourseOrderService;
import cn.wolfcode.crud.service.ICustomerService;
import cn.wolfcode.crud.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CourseOrderServiceImpl implements ICourseOrderService {

    @Autowired
    private CourseOrderMapper courseOrderMapper;

    @Autowired
    private ICustomerService customerService;

    @Override
    public void save(CourseOrder courseOrder) {

        courseOrder.setInputTime(new Date());
        //修改当前潜在用户的状态
        Long id = courseOrder.getCustomer().getId();
        Customer customer = customerService.get(id);
        customer.setStatus(Customer.STATUS_NORMAL);
        customerService.updateStatus(customer);


        courseOrderMapper.insert(courseOrder);
    }

    @Override
    public void update(CourseOrder courseOrder) {
        courseOrderMapper.updateByPrimaryKey(courseOrder);
    }

    @Override
    public CourseOrder get(Long id) {
        return courseOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CourseOrder> listAll() {
        return courseOrderMapper.selectAll();
    }

    @Override
    public PageInfo<CourseOrder> query(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<CourseOrder>  CourseOrder= courseOrderMapper.selectForList(qo);
        return new PageInfo<>(CourseOrder);
    }
}
